/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.ipaddress;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Properties;

public class IPAdress {
String IPHostName,HostAddress;

  public String IPAddress() {
    try {
      IPHostName=(InetAddress.getLocalHost().getHostName());
      HostAddress=(InetAddress.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
return HostAddress;
  }
public String getMyPublicIP(){
        try {
            String url = "http://checkip.dyndns.org:8245/",
               proxy = "proxy.up.ac.th",
               port = "8080",
               username = "up\\51286885",
               password = "7489250721";

            //URL autoIP = new URL("http://checkip.dyndns.org:8245/");
            System.out.println("Get URL");
           
          URL server = new URL(url);

            Authenticator.setDefault(new authenticator(
                        username,password));
            Properties systemProperties = System.getProperties();
            systemProperties.setProperty("http.proxyHost",proxy);
            systemProperties.setProperty("http.proxyPort",port);
            HttpURLConnection connection = (HttpURLConnection)server.openConnection();
            connection.connect();

             
                
          BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()));//.openStream()
            //System.out.println("Open  URL");
            String ip_address = (in.readLine()).trim();
           String publicip="";int start=0;
           for(int i=0;i<ip_address.length();i++){
               if(ip_address.charAt(i)==':'){
                start=1;
               }
               
               if(start==1){
                   if((ip_address.charAt(i)>='0'&&ip_address.charAt(i)<='9')||ip_address.charAt(i)=='.')
                      publicip+=ip_address.charAt(i);
               }
           }
        //   System.out.println(ip_address.length());
                   return publicip;

         }catch (Exception e){
	    	e.printStackTrace();
                

            return "No Connection";
	    }
    }
    

}